# -- coding: utf-8 --
# -- coding: utf-8 --

from flask import Flask, request, render_template, redirect
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526.AuthorizeSecurityGroupRequest import AuthorizeSecurityGroupRequest
from aliyunsdkrds.request.v20140815.ModifySecurityIpsRequest import ModifySecurityIpsRequest
from aliyunsdkrds.request.v20140815.DescribeDBInstanceIPArrayListRequest import DescribeDBInstanceIPArrayListRequest


host = 'http://0.0.0.0:7090/'
app = Flask (__name__)

@app.route('/')
def index():
    return render_template('Ruoindex.html')


@app.route('/RDS')
def Rdsindex():
    return render_template('RuoRds.html')

@app.route('/AddRDS',methods=['POST'])
def AddRDS():
    # !/usr/bin/env python
    # coding=utf-8
    IP = request.form.get('IP')

    client = AcsClient('LTAI3m2TZPbnLFP8', 'sgtiIV0Z8cZ8iquQcWNK1cE1nUxE5e', 'cn-hangzhou')

    aliyunrequest = ModifySecurityIpsRequest()
    aliyunrequest.set_accept_format('json')

    aliyunrequest.set_DBInstanceId("rm-bp19t127iujao8u2n")
    aliyunrequest.set_SecurityIps(IP)
    aliyunrequest.set_DBInstanceIPArrayName("gaoyu_home")
    aliyunrequest.set_ModifyMode("Append")

    response = client.do_action_with_exception(aliyunrequest)
    # python2:  print(response)
    return (str(response, encoding='utf-8'))

@app.route('/QuerryRDS',methods=['POST'])
def QuerryRds():
    client = AcsClient('LTAI3m2TZPbnLFP8', 'sgtiIV0Z8cZ8iquQcWNK1cE1nUxE5e', 'cn-hangzhou')

    request = DescribeDBInstanceIPArrayListRequest()
    request.set_accept_format('json')

    request.set_DBInstanceId("rm-bp19t127iujao8u2n")

    response = client.do_action_with_exception(request)
    # python2:  print(response)
    # return str((response[5]))
    return (dict(response))


@app.route('/AddSG',methods=['POST'])
def AddSG():
    global response
    # NetType = request.form.get('NetType')
    # SecurityGroupID = request.form.get('SecurityGroupID')
    # Port = request.form.get('Port')
    Port = '22/22'
    SecurityGroupID ='sg-bp152485a5pznfvc9xoo'
    NetType='tcp'
    IP = request.form.get('IP')


    print(NetType,SecurityGroupID,Port,IP)

    # 错client = AcsClient('LTAI3m2TZPbnLFP8', 'sgtiIV0Z8cZ8iquQcWNK1cE1nUxE5e1', 'cn-hangzhou')
    client = AcsClient('LTAI3m2TZPbnLFP8', 'sgtiIV0Z8cZ8iquQcWNK1cE1nUxE5e', 'cn-hangzhou')

    aliyunrequest = AuthorizeSecurityGroupRequest()
    aliyunrequest.set_accept_format('json')

    aliyunrequest.set_SecurityGroupId(SecurityGroupID)
    aliyunrequest.set_IpProtocol(NetType)
    aliyunrequest.set_PortRange(Port)
    aliyunrequest.set_SourceCidrIp(IP)


    response = client.do_action_with_exception(aliyunrequest)
    return (str(response[1], encoding='utf-8'))



app.run(port=7090 ,host='0.0.0.0')


# python2:  print(response)
# print(str(response, encoding='utf-8'))